﻿using System;
using System.Collections.Generic;
using System.Data;
using VOL.Entity.DomainModels;

namespace VOL.WebApi.VOL.WebApi.Operator.Wms
{
    public class T_VIEW_CONOperator
    {
        public static bool SynchronizeContracts()
        {

            bool resutl = false;
            try
            {
                string zsql;
                DateTime datetime = GetSynchronizeContractsDateStamp();
                zsql = @"SELECT   CID, T_APP_DPT_CID, T_APP_DPT_NAME, T_APP_USER_CODE, T_APP_USER_NAME, 
                T_APP_USER_TEL, T_APP_USER_DATE, T_EXECUTOR_NAME, T_EXECUTOR_CODE, EXP40, T_DRAFT_NAME, 
                T_RELATED_NAME, T_RELATED_CID, T_CONTRACT_CODE, T_CONTRACT_NAME, T_CONTRACT_TYPE_NAME, 
                T_CONTRACT_TYPE_CID, T_CONTRACT_MONEY, T_MONEY_UNIT, T_MONEY_CAPITAILZATION, T_RATE, 
                T_RATE_BEFOR_MONEY, T_TEAN_TYPE, T_START_DATE, T_END_DATE, T_TEAN, T_TEAN_UNIT, T_PAY_WAY, 
                T_PAY_WAY_CID, T_PROJECT_NAME, T_PROJECT_CID, T_PROJECT_USER_NAME, T_PROJECT_USER_CODE, 
                T_REMARK, T_SUBJECT_NAME, T_SUBJECT_CID, T_SEAL, T_SEAL_PATH, T_GOOD_CID, T_ORDER, T_GOOD_CODE, 
                T_GOOD_NAME, T_GOOD_MODEL, T_GOOD_PRICE, T_GOOD_NUM, T_GOOD_UNIT, T_GOOD_MONEY, T_GOOD_TIME, 
                T_GOOD_ADDRESS, T_GOOD_REMARK, T_GOOD_TRANSACTION_NUM, T_GOOD_EXP1, T_GOOD_EXP2, 
                T_GOOD_EXP3, T_GOOD_EXP4, T_RELATIVE_NAME  FROM[DASContract].[dbo].[VIEW_CON] where T_APP_USER_DATE >='" + datetime + "' and [T_GOOD_CODE] is not null ";

                DataTable dt = new DataTable();
                dt = PurchasePub.SqlQuery(zsql);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!IsExistContract(dt.Rows[i]["CID"].ToString(), dt.Rows[i]["T_GOOD_CID"].ToString()))
                        {
                            string sql = @"insert into T_VIEW_CON(guid,CID, T_APP_DPT_CID, T_APP_DPT_NAME, T_APP_USER_CODE, T_APP_USER_NAME, 
                T_APP_USER_TEL, T_APP_USER_DATE, T_EXECUTOR_NAME, T_EXECUTOR_CODE, EXP40, T_DRAFT_NAME, 
                T_RELATED_NAME, T_RELATED_CID, T_CONTRACT_CODE, T_CONTRACT_NAME, T_CONTRACT_TYPE_NAME, 
                T_CONTRACT_TYPE_CID, T_CONTRACT_MONEY, T_MONEY_UNIT, T_MONEY_CAPITAILZATION, T_RATE, 

                T_RATE_BEFOR_MONEY, T_TEAN_TYPE, T_START_DATE, T_END_DATE, T_TEAN, T_TEAN_UNIT, T_PAY_WAY, 
                T_PAY_WAY_CID, T_PROJECT_NAME, T_PROJECT_CID, T_PROJECT_USER_NAME, T_PROJECT_USER_CODE, 

                T_REMARK, T_SUBJECT_NAME, T_SUBJECT_CID, T_SEAL, T_SEAL_PATH, T_GOOD_CID, T_ORDER, T_GOOD_CODE, 
                T_GOOD_NAME, T_GOOD_MODEL, T_GOOD_PRICE, T_GOOD_NUM, T_GOOD_UNIT, T_GOOD_MONEY, T_GOOD_TIME, 
                T_GOOD_ADDRESS, T_GOOD_REMARK, T_GOOD_TRANSACTION_NUM, T_GOOD_EXP1, T_GOOD_EXP2, 
                T_GOOD_EXP3, T_GOOD_EXP4, T_RELATIVE_NAME  )";
                            string guid = Guid.NewGuid().ToString();
                            sql += " values('" + guid + "','" + dt.Rows[i]["CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_APP_DPT_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_APP_DPT_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_APP_USER_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_APP_USER_NAME"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_APP_USER_TEL"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_APP_USER_DATE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_EXECUTOR_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_EXECUTOR_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["EXP40"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_DRAFT_NAME"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RELATED_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RELATED_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_CONTRACT_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_CONTRACT_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_CONTRACT_TYPE_NAME"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_CONTRACT_TYPE_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_CONTRACT_MONEY"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_MONEY_UNIT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_MONEY_CAPITAILZATION"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RATE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RATE_BEFOR_MONEY"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_TEAN_TYPE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_START_DATE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_END_DATE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_TEAN"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_TEAN_UNIT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_PAY_WAY"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_PAY_WAY_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_PROJECT_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_PROJECT_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_PROJECT_USER_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_PROJECT_USER_CODE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_REMARK"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_SUBJECT_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_SUBJECT_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_SEAL"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_SEAL_PATH"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_CID"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_ORDER"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_CODE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_GOOD_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_MODEL"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_PRICE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_NUM"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_UNIT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_MONEY"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_TIME"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_GOOD_ADDRESS"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_REMARK"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_TRANSACTION_NUM"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_EXP1"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_EXP2"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_GOOD_EXP3"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_EXP4"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RELATIVE_NAME"].ToString() + "')";
                            WmsPub.SqlExecute(sql);
                        }

                    }
                }
                return true;

            }
            catch
            {
                return resutl;
            }
            finally
            {

            }


        }

        internal static T_VIEW_CON GetPageDataByIdOperator(string? wheres)
        {
            string sql;
            sql = @"SELECT  guid, CID, T_APP_DPT_CID, T_APP_DPT_NAME, T_APP_USER_CODE, T_APP_USER_NAME, 
                T_APP_USER_TEL, T_APP_USER_DATE, T_EXECUTOR_NAME, T_EXECUTOR_CODE, EXP40, T_DRAFT_NAME, 
                T_RELATED_NAME, T_RELATED_CID, T_CONTRACT_CODE, T_CONTRACT_NAME, T_CONTRACT_TYPE_NAME, 
                T_CONTRACT_TYPE_CID, T_CONTRACT_MONEY, T_MONEY_UNIT, T_MONEY_CAPITAILZATION, T_RATE, 
                T_RATE_BEFOR_MONEY, T_TEAN_TYPE, T_START_DATE, T_END_DATE, T_TEAN, T_TEAN_UNIT, T_PAY_WAY, 
                T_PAY_WAY_CID, T_PROJECT_NAME, T_PROJECT_CID, T_PROJECT_USER_NAME, T_PROJECT_USER_CODE, 
                T_REMARK, T_SUBJECT_NAME, T_SUBJECT_CID, T_SEAL, T_SEAL_PATH, T_GOOD_CID, T_ORDER, T_GOOD_CODE, 
                T_GOOD_NAME, T_GOOD_MODEL, T_GOOD_PRICE, T_GOOD_NUM, T_GOOD_UNIT, T_GOOD_MONEY, T_GOOD_TIME, 
                T_GOOD_ADDRESS, T_GOOD_REMARK, T_GOOD_TRANSACTION_NUM, T_GOOD_EXP1, T_GOOD_EXP2, 
                T_GOOD_EXP3, T_GOOD_EXP4, T_RELATIVE_NAME 
FROM      T_VIEW_CON   where " + wheres;

            DataTable tb = WmsPub.SqlQuery(sql);
            if (tb != null)
            {
                List<T_VIEW_CON> t_view_cons = tb.GetTableToList<T_VIEW_CON>();
                return t_view_cons[0];
            }
            else
            {
                T_VIEW_CON t_view_con = new T_VIEW_CON();
                return t_view_con;
            }



        }

        private static bool IsExistContract(string? cid, string? t_good_cid)
        {
            if (t_good_cid != "")
            {
                return false;
            }
            else
            {
                string sql;
                sql = "select T_RM_UNI_CODE from [T_VIEW_CON] where cid='" + cid + "' and t_good_cid='" + t_good_cid + "'  ";
                DataTable tb = WmsPub.SqlQuery(sql);
                if (tb != null)
                {
                    if (tb.Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static DateTime GetSynchronizeContractsDateStamp()
        {
            string sql;
            sql = "select  isnull(max(T_APP_USER_DATE),'') from [T_VIEW_CON]   ";
            DataTable tb = WmsPub.SqlQuery(sql);
            if (tb != null)
            {
                if (tb.Rows[0][0].ToString() != "")
                {
                    return DateTime.Parse(tb.Rows[0][0].ToString());
                }
                else
                {
                    return DateTime.Parse("2001-01-01");
                }

            }
            else
            {
                return DateTime.Parse("2001-01-01");
            }
        }
    }
}
